package com.example.smartcity.ui.dashboard;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.smartcity.R;
import com.example.smartcity.ui.dashboard.Fragment.AccountMangerFragment;
import com.google.android.material.textfield.TextInputEditText;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class AddAccountGroup extends AppCompatActivity {

    private Spinner AccountType;
    private Spinner GitType;
    private TextInputEditText newAccountNumber;
    static List<String> list = Arrays.asList("我家","父母","房东","朋友","自定义");
    List<String> GitTypeList = Arrays.asList("水费","电费","燃气费","物业费");
    List AccountTypeList;
    private TextView TV_Commit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_account_group);
        AccountType = findViewById(R.id.AccountType);
        GitType = findViewById(R.id.GitType);
        newAccountNumber = findViewById(R.id.newAccountNumber);
        TV_Commit = findViewById(R.id.TV_commit);
        initView();
    }
    public void setBack(View view){
        this.finish();
    }
    public void initView(){
        AccountTypeList= new ArrayList(list);
        AccountType.setAdapter(new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_dropdown_item,AccountTypeList));
        GitType.setAdapter(new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_dropdown_item,GitTypeList));
        AccountType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                View v = LayoutInflater.from(getBaseContext()).inflate(R.layout.selfdialoglayout,null,false);
                EditText editText = v.findViewById(R.id.ED_Group);
                if (position==AccountTypeList.size()-1){
                    new AlertDialog.Builder(AddAccountGroup.this)
                            .setView(v)
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    AccountType.setSelection(AccountTypeList.size()-1);
                                    AccountTypeList.add(AccountTypeList.size()-1,editText.getText().toString());
                                }
                            })
                            .show();
                    AccountType.setAdapter(new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_dropdown_item,AccountTypeList));
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        TV_Commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AccountMangerFragment f = new AccountMangerFragment();
                f.addMethod(AccountType.getSelectedItem().toString(),GitType.getSelectedItem().toString(),newAccountNumber.getText().toString());
                AddAccountGroup.this.finish();
            }
        });
    }

}